<?php
namespace Admin\Model;
use Think\Model;
class CodeModel extends Model {
    public function makefile($obj,$name,$array){
       $make='make'.$obj;
       $name=ucfirst($name);
       $this->$make($name,$array);
    }
    public function makecontroller($name='',$array=''){
        $path='./Application/Home/Controller/'.$name.'Controller.php';
        if(file_exists($path)){
           die('文件已经存在，请手动安装');
        }
        $string='<?php
namespace Home\Controller;
use Think\Controller;
class '.$name.'Controller extends Controller {
}              ';
      $f=fopen($path,'w');
      fwrite($f,$string);

    }

    public function makemodel($name='',$array=''){
      $path='./Application/Home/Model/'.$name.'Model.class.php';
      if(file_exists($path)){
           die('文件已经存在，请手动安装');
      }
      $validate='';
      $auto='';
      foreach ($array as $value) {
          $validate.="array('".$value['field']."','require','".$value['comment']."必须！',self::EXISTS_VALIDATE ),\n         ";
          if(strpos($value['field'],'createtime')){
              $auto.="array('".$value['field']."','time',self:: MODEL_INSERT,'function'),\n         ";
          }
          if(strpos($value['field'],'updatetime')){
              $auto.="array('".$value['field']."','time',self:: MODEL_INSERT,'function'),\n         ".
                     "array('".$value['field']."','time',self:: MODEL_UPDATE,'function'),\n         ";
          }
          
      }
      $baseString='<?php
namespace Home\Model;
use Think\Model;
class '.$name.'Model extends Model {
    //自动验证
    protected $_validate = array(
         '.$validate.'
    );
    // 自动填充设置
    protected $_auto = array(
         '.$auto.'
    );
}';

      $path='./Application/Home/Model/'.$name.'Model.class.php';
      $f=fopen($path,'w');
      fwrite($f,$baseString);

    }

    public function makeviewmodel($name='',$array=''){
      $path='./Application/Home/Model/'.$name.'ViewModel.class.php';
      if(file_exists($path)){
           //die('文件已经存在，请手动安装');
      }
      //视图模型的数组
      $viewFields='';
      //需要关联的数组
      $ma=$this->view($name);
      var_dump($aa);
      foreach ($ma as $key=> $value) {
           $mb=$value['field'];
           if($key!=$name){
                $mb['_on']="_on'=>'".$name.".".$value['key']."=".$key.".".$value['key'];
           }
           $mb['_type']="_type'=>'left";
           foreach ($mb as &$v) {
               $v="'".$v."'";
           }
           $field=implode(',',$mb);
           $viewFields.="
                '".$key."'=>array(".$field."),\n        
           ";
      }
      $baseString='<?php
namespace Home\Model;
use Think\Model;
class '.$name.'ViewModel extends ViewModel {
    //视图模型
    protected $viewFields = array(
         '.$viewFields.'
    );
    
}';
     
      $f=fopen($path,'w');
      fwrite($f,$baseString); 
    }
    public function view($table){
        //$table='article';
        $nModel=D($table);
        $foregin=array();
        //当前表的字段前缀
        $nPrefix=$this->prefix($table);
        $nField=$nModel->getDbFields();
        foreach ($nField as $value) {
          $pos=strpos($value,$nPrefix);
           if($pos===false){
               array_push($foregin,$value);
           } 
        }
        //获取所有表
        $tables=$this->gettables();
        $field=array();//定义字段数组
        foreach ($tables as $value) {
             $ModelName=$this->getModelName($value);
             $ModelField=M($ModelName)->getDbFields();
             $ModelKey=M($ModelName)->getPk();
             if($ModelName==$table||(array_intersect($ModelField,$foregin) && in_array($ModelKey,$foregin))){
                 $Mode[$ModelName]['field']=$ModelField;
                 $Mode[$ModelName]['key']=$ModelKey;
             }
        }
        //返回
        return $Mode;

    }
    public function gettables(){
       $model=M();
       $tables=$model->query('show tables');
       $tb=$this->dearray('tables_in_ushow',$tables);
       $tb=empty($tb)?'':$tb;
       return $tb;
    }
    public function getfields($tbname=''){
       $model=M();
       $field=$model->query('SHOW FULL COLUMNS FROM `he_'.$tbname.'`');
       return $field;
    }

    //获取字段前缀
    public function prefix($table){
       $foreignKey=D($table)->getPk();
       $foreignKey=explode('_',$foreignKey);
       $foreignKey=$foreignKey[0].'_';
       return $foreignKey;
       
    }
    public function dearray($id,$array){
       $return=array();
       foreach($array as $value){
          $return[]=$value[$id];  
       }
       return $return;
    }
    //获取模型名称
    function getModelName($tbname){
         $tbname=str_replace(C('DB_PREFIX'),'',$tbname);
         $array=explode('_',$tbname);
         foreach ($array as &$value) {
             $value=ucfirst($value);
         }
         $modelName=implode('',$array);
         return $modelName;
    }

}